<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">控制台</el-breadcrumb-item>
      <el-breadcrumb-item><b>收费管理</b></el-breadcrumb-item>
      <el-breadcrumb-item>收费明细管理</el-breadcrumb-item>
    </el-breadcrumb>
    <hr>

    <el-card style="font-size: 20px;font-family: 'Helvetica Nee',serif">
      <b>收费明细管理</b>
      <p style="font-size: 15px">用于展示物业收费明细列表信息，以及增加、修改、删除等操作</p>
    </el-card>

    <el-card class="box-card">
      <div class="card-header">
        <el-row>
          <!-- 添加按钮 -->
          <el-col :span="18">
            <Button type="success" icon="md-add" @click="dialogFormVisibleAdd = true">添加</Button>
            <span style="margin-left: 10px;"></span>
            <Button v-if="showDeleteButton" type="error" icon="md-trash" @click="deleteByIds()">批量删除</Button>
          </el-col>
          <!-- 搜索框 -->
          <el-col :span="4">
            <Input search style="width: 260px;" enter-button placeholder="请输入搜索的内容" @on-search="search()"/>
          </el-col>
          <!-- 根据小区查询下拉框 -->
          <el-col :span="2" style="text-align: right;">
            <Dropdown @on-click="selectByCommunity">
              <Button type="primary">
                根据小区查询 <Icon type="ios-arrow-down"></Icon>
              </Button>
              <DropdownMenu slot="list">
                <DropdownItem :name="null">
                  所有小区
                </DropdownItem>
                <DropdownItem v-for="community, index in communityList" :key="index" :name="community.code">
                  {{ community.name }}
                </DropdownItem>
              </DropdownMenu>
            </Dropdown>
          </el-col>
        </el-row>
      </div>

      <el-table v-loading="loadTable" :border="true" ref="multipleTable" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column fixed prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="communityName" label="所属小区" width="200" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectName" label="收费项目" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="houseHolder" label="业主" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="amountTotal" label="应收金额（¥）" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="amountPaid" label="实收金额（¥）" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="remark" label="备注" width="150" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="gmtCreate" label="缴费时间" width="180" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column align="center" label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <ButtonGroup>
              <Button type="info" icon="md-create" @click="showUpdateDialog(scope.row)"></Button>
              <Button type="error" icon="md-trash" @click="deleteIds = [scope.row.id]; deleteByIds()"></Button>
            </ButtonGroup>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <!-- 分页查询 -->
        <Page :total="total" show-elevator show-sizer :current="currentPage" :page-size="pageSize" @on-change="changePage" @on-page-size-change="changePageSize"/>
      </div>
    </el-card>

    <el-dialog :close-on-click-modal="false" title="新增收费明细" :visible.sync="dialogFormVisibleAdd" center>
      <el-form :model="editFormAdd">
        <el-form-item label="所属小区" :label-width="formLabelWidth" required>
          <el-select v-model="editFormAdd.communityCode" placeholder="请选择" border>
            <el-option v-for="item, index in communityList" :key="index" :label="item.name" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费项目" :label-width="formLabelWidth" required>
          <el-select v-model="editFormUpdate.projectCode" placeholder="请选择" border>
            <el-option v-for="item, index in projectList" :key="index" :label="item.name" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业主" :label-width="formLabelWidth" required>
          <el-select v-model="editFormUpdate.houseCode" placeholder="请选择" border>
            <el-option v-for="item, index in peopleList" :key="index" :label="item.peopleName" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应收金额（¥）" :label-width="formLabelWidth" required>
          <el-input v-model="editFormAdd.amountTotal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="实收金额（¥）" :label-width="formLabelWidth" required>
          <el-input v-model="editFormAdd.amountPaid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" required>
          <el-input v-model="editFormAdd.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="缴费时间" :label-width="formLabelWidth" required>
            <el-date-picker
                v-model="editFormAdd.gmtCreate"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" title="修改收费明细" :visible.sync="dialogFormVisibleUpdate" center>
      <el-form :model="editFormUpdate">
        <el-form-item label="所属小区" :label-width="formLabelWidth" required>
          <el-select v-model="editFormUpdate.communityCode" placeholder="请选择" border>
            <el-option v-for="item, index in communityList" :key="index" :label="item.name" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费项目" :label-width="formLabelWidth" required>
          <el-select v-model="editFormUpdate.projectCode" placeholder="请选择" border>
            <el-option v-for="item, index in projectList" :key="index" :label="item.name" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业主" :label-width="formLabelWidth" required>
          <el-select v-model="editFormUpdate.houseCode" placeholder="请选择" border>
            <el-option v-for="item, index in peopleList" :key="index" :label="item.peopleName" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应收金额（¥）" :label-width="formLabelWidth" required>
          <el-input v-model="editFormUpdate.amountTotal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="实收金额（¥）" :label-width="formLabelWidth" required>
          <el-input v-model="editFormUpdate.amountPaid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" required>
          <el-input v-model="editFormUpdate.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="缴费时间" :label-width="formLabelWidth" required>
          <el-date-picker
              v-model="editFormUpdate.gmtCreate"
              type="datetime"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleUpdate = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 返回顶部按钮 -->
    <BackTop></BackTop>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "index",
  data() {
    return {
      loadTable: false,
      showDeleteButton: false,
      deleteIds: [],
      editFormUpdate: {},
      editFormAdd: {},
      searchText: "",
      dialogFormVisibleUpdate: false,
      dialogFormVisibleAdd: false,
      communityList: [],
      peopleList: [],
      projectList: [],
      formLabelWidth: '120px',
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    // 搜索方法
    search() {

    },
    // 添加数据方法
    add() {
      if (Object.keys(this.editFormAdd).length === 0) {
        this.$message.error("请填写内容！");
        return;
      }
      let url = this.host + "/chargeDetails/add";
      this.axios.post(url, JSON.stringify(this.editFormAdd), {
        headers: {
          "Content-Type": "application/json"
        }}).then((response) => {
        if (response.data.statusCode == 2000) {
          this.$message.success(response.data.data);
          this.dialogFormVisibleAdd = false;
          this.loadDataList();
        } else {
          this.$message.error(response.data.message);
        }
      });
    },
    // 批量删除
    deleteByIds() {
      // 打开加载状态
      this.loadTable = true;
      let url = this.host + "/chargeDetails/delete?" + this.deleteIds.map(item => `ids=${encodeURIComponent(item)}`).join('&');
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.$message.success(response.data.data);
          this.loadDataList();
        } else {
          this.$message.error("删除失败，请稍后再试！");
        }
      });
      // 取消加载状态
      this.loadTable = false;
      this.deleteIds = [],
          this.showDeleteButton = false;
    },
    // 表格多选选择后触发的方法
    handleSelectionChange(rows) {
      if (rows.length === 0) {
        this.showDeleteButton = false;
        return;
      }
      this.showDeleteButton = true;
      for (let item of rows) {
        this.deleteIds.push(item.id);
      }
    },
    // 打开弹窗方法
    showUpdateDialog(row) {
      this.dialogFormVisibleUpdate = true;
      this.editFormUpdate = row;
    },
    // 修改数据的方法
    update() {
      if (Object.keys(this.editFormUpdate).length === 0) {
        this.$message.error("请填写内容！");
        return;
      }
      let url = this.host + "/chargeDetails/update";
      this.axios.post(url, JSON.stringify(this.editFormUpdate), {
        headers: {
          "Content-Type": "application/json"
        }}).then((response) => {
        if (response.data.statusCode == 2000) {
          this.$message.success(response.data.data);
          this.dialogFormVisibleUpdate = false;
          this.loadDataList();
        } else {
          this.$message.error(response.data.message);
        }
      });
    },
    // 选择小区后调用的方法
    selectByCommunity(communityCode) {
      this.communityCode = communityCode;
      this.loadDataList();
    },
    // 改变第几页后调用的方法
    changePage(pageNum) {
      this.currentPage = pageNum;
      this.loadDataList();
    },
    // 改变每页多少条数据后调用的方法
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.loadDataList();
    },
    // 加载当前模块数据
    loadDataList() {
      this.loadTable = true;
      let url = this.host + "/chargeDetails/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize;
      if (this.communityCode != null) {
        url = this.host + "/chargeDetails/list?pageNum=" + this.currentPage + "&pageSize=" + this.pageSize + "&communityCode=" + this.communityCode;
      }
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.tableData = response.data.data.records;
          this.currentPage = response.data.data.current;
          this.pageSize = response.data.data.size;
          this.total = response.data.data.total;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
      this.loadTable = false;
    },
    // 加载所有人员数据
    loadPeopleList() {
      let url = this.host + "/owners/list";
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.peopleList = response.data.data.records;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
    },
    // 加载所有小区数据
    loadCommunityList() {
      let url = this.host + "/community/list";
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.communityList = response.data.data.records;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
    },
    // 加载所有收费项目数据
    loadProjectList() {
      let url = this.host + "/chargeProject/list";
      this.axios.get(url).then((response) => {
        if (response.data.statusCode == 2000) {
          this.projectList = response.data.data.records;
        } else {
          this.$message.error("加载数据出错了！");
        }
      });
    }
  },
  mounted() {
    // 页面初始化后调用加载本模块数据和小区等的所有数据
    this.loadDataList();
    this.loadCommunityList();
    this.loadPeopleList();
    this.loadProjectList();
  }
}
</script>

<style scoped>
.card-header {
  margin-bottom: 20px;
}

.block {
  text-align: center;
  margin-top: 10px;
}

.el-select {
  margin-left: 15px;
  margin-right: 30px;
}

.el-date-picker {
  margin-left: 50px;
}
</style>
